<template>
  <div class="app-container">
    <el-form v-model="demo" label-width="90px">
        <el-form-item prop="company" label="公司">
          <el-input v-model="demo.company" icon="search" name="title" placeholder="请输入"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="8">
          <el-form-item prop="displayName" label="负责人" label-width="90px">
            <el-input v-model="demo.displayName" icon="search" name="title" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="cellphone" label="手机号" label-width="90px">
            <el-input v-model="demo.cellphone" icon="search" name="title" placeholder="请输入手机号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="identityCard" label="身份证" label-width="90px">
            <el-input v-model="demo.identityCard" icon="search" name="title" placeholder="请输入身份证号"></el-input>
          </el-form-item>
        </el-col>
        </el-row>
        <el-form-item prop="name" label="属性">
          <el-input v-model="demo.agentOid" icon="search" name="title" placeholder="服务商" :disabled="true">服务商</el-input>
        </el-form-item>
        <el-row>
          <el-col :span="8">
          <el-form-item prop="region" label="授权区域" label-width="90px">
            <el-select v-model="demo.province" placeholder="请选择" :disabled="AgentShow" style="width: 160px;" @change="cityShow">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="name" label="城市" label-width="90px">
            <el-select v-model="demo.city" placeholder="请选择" :disabled="AgentShow" style="width: 160px;" @change="countyShow">
              <el-option
                v-for="item in citys"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="name" label="县" label-width="90px">
            <el-select v-model="demo.district" placeholder="请选择" style="width: 160px;">
              <el-option
                v-for="item in county"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        </el-row>
        <el-row>
          <!-- <el-col :span="8">
          <el-form-item prop="name" label="授权总数量" label-width="90px">
            <el-input v-model="demo.termCode" icon="search" name="title" placeholder="请输入" :disabled="true"></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="8">
          <el-form-item prop="name" label="教师数量" label-width="90px">
            <el-input v-model="demo.activationTeacherMax" icon="search" name="title" placeholder="请输入" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="name" label="学生数量" label-width="90px">
            <el-input v-model="demo.activationStudentMax" icon="search" name="title" placeholder="请输入" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        </el-row>
        <el-row>
        <el-col :span="8">
          <el-form-item prop="name" label="增加教师数量" label-width="100px">
            <el-input v-model="demo.activationTeacherAdd" icon="search" name="title" type="number" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="name" label="增加学生数量" label-width="100px">
            <el-input v-model="demo.activationStudentAdd" icon="search" name="title" type="number" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
          <el-form-item prop="name" label="服务时间" label-width="90px">
          <el-input v-model="demo.years" icon="search" name="title" type="number" placeholder="请输月份"></el-input>
          <el-input v-model="demo.agentOid" type="hidden"></el-input>
            <!-- <el-select v-model="demo.years" placeholder="请选择" style="width: 160px;">
              <el-option
                v-for="item in years"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select> -->
          </el-form-item>
        </el-col>
        </el-row>
          <el-form-item prop="name" label="授权方案" label-width="90px">
             <el-checkbox-group v-model="demo.checkedCities">
              <el-checkbox v-for="city in cities" :label="city.key" :key="city.key">{{city.value}}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        </el-row>
      </el-form>
       <el-button type="primary" @click="ServiceManagementAdd()" style="margin-top: 8px;margin-left: 20px;">确定</el-button>
  </div>
</template>

<script>
import { fetchList } from "@/api/article";
import axios from "axios";
import api from "@/api/servier";
import { Message } from "element-ui";
const cityOptions = [];
export default {
  name: "InlineEditTable",
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "info",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  data() {
    return {
      district: "",
      demo: {
        name: "",
        packageName: "",
        region: "",
        checkedCities: [],
        agentOid: "",
        province: "",
        agentOid: "",
        termCode: "",
        district: "",
        city: "",
        createdTime: ""
      },
      years: [
        {
          value: "1",
          label: "1年"
        },
        {
          value: "2",
          label: "2年"
        }
      ],
      list: [],
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      value: "",
      fileData: [],
      options: [],
      county: [],
      citys: [],
      AgentShow: false,
      checkAll: false,
      cities: cityOptions,
      isIndeterminate: true
    };
  },
  created() {},
  mounted() {
    this.GetAgentRegions();
    api.regions().then(response => {
      // this.options = response.data
      response.map((item, index) => {
        this.options.push({
          label: item.label,
          value: item.code
        });
      });
    });
    this.schemesShow();
    this.ServiceManagementShow();
  },
  methods: {
    GetAgentRegions() {
      let role = localStorage.getItem("role");
      if (role == "代理商") {
        this.AgentShow = true;
      } else {
        this.AgentShow = false;
      }
    },
    schemesShow() {
      axios.get("/scheme/query").then(response => {
        console.log(response.data);
        cityOptions.splice(0, cityOptions.length);
        response.data.map((item, index) => {
          cityOptions.push({
            key: item.oid,
            value: item.name
          });
        });
      });
    },
    ServiceManagementShow() {
      api.serviceGet(this.$route.query.oid).then(response => {
        let data = response;
        this.demo.username = data.username;
        this.demo.company = data.company;
        this.demo.activationMax = data.activationMax;
        this.demo.activationStudentMax = data.activationStudentMax;
        this.demo.activationTeacherMax = data.activationTeacherMax;
        this.demo.displayName = data.displayName;
        this.demo.cellphone = data.cellphone;
        this.demo.identityCard = data.identityCard;
        this.demo.province = data.province.code;
        this.demo.password = data.password;
        this.demo.years = data.monthCount;
        this.demo.agentOid = data.agentOid;
        this.demo.createdTime = data.createdTime;
        let checkedCitiesData = [];
        data.schemes.map((item, index) => {
          checkedCitiesData.push(item.oid);
        });
        this.demo.checkedCities = checkedCitiesData;
        this.demo.termCode =
          Number(data.activationStudentMax) + Number(data.activationTeacherMax);
        if (data.province.code) {
          api.city(data.province.code).then(response => {
            response.map((item, index) => {
              this.citys.push({
                label: item.label,
                value: item.code
              });
            });
            if (data.city.code) {
              this.demo.city = data.city.code;
            }
          });
        }
        if (data.city.code) {
          api.county(data.city.code).then(response => {
            response.map((item, index) => {
              this.county.push({
                label: item.label,
                value: item.code
              });
            });
            if (data.district.code) {
              this.demo.district = data.district.code;
            }
          });
        }
      });
    },
    ServiceManagementAdd() {
      let checkgroup = [];
      this.demo.checkedCities.map((item, index) => {
        checkgroup.push({ oid: Number(item) });
      });
      let data = {
        activationStudentMax: this.demo.activationStudentMax,
        activationTeacherMax: this.demo.activationTeacherMax,
        activationTeacherAdd: this.demo.activationTeacherAdd,
        activationStudentAdd: this.demo.activationStudentAdd,
        company: this.demo.company,
        displayName: this.demo.displayName,
        cellphone: this.demo.cellphone,
        identityCard: this.demo.identityCard,
        province: {
          code: this.demo.province
        },
        district: {
          code: this.demo.district
        },
        city: {
          code: this.demo.city
        },
        agentOid: this.demo.agentOid,
        schemes: checkgroup,
        monthCount: this.demo.years,
        oid: Number(this.$route.query.oid),
        createdTime: this.demo.createdTime
      };
      console.log(data);
      api.serviceEdit(data).then(({ success, data }) => {
        this.$message({
          showClose: true,
          message: "添加成功！",
          type: "success"
        });
        this.$router.push("/ServiceManagement");
      });
    },
    cityShow(value, data) {
      api.city(value, data).then(response => {
        if (this.citys.length > 0) {
          this.citys = [];
          this.demo.city = "请选择";
        }
        if (this.county.length > 0) {
          this.county = [];
          this.demo.county = "请选择";
        }
        response.map((item, index) => {
          this.citys.push({
            label: item.label,
            value: item.code
          });
        });
      });
    },
    countyShow(value, data) {
      api.county(value, data).then(response => {
        if (this.county.length > 0) {
          this.county = [];
          this.demo.county = "请选择";
        }
        response.map((item, index) => {
          this.county.push({
            label: item.label,
            value: item.code
          });
        });
      });
    }
  }
};
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
#upClass {
  width: 420px;
  height: 35px;
  -webkit-appearance: none;
  background-color: #fff;
  background-image: none;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  color: #606266;
  display: inline-block;
  font-size: inherit;
  line-height: 40px;
  outline: 0;
  padding: 0 15px;
  -webkit-transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  width: 100%;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
